﻿using CocoaAni.Net.WebSockets.Server.Attributes;
using CocoaAni.Net.WebSockets.Server.Enums;
using System.Reflection;
using CocoaAni.Net.WebSockets.Server.ArgAdapters.Base;
using System;

namespace CocoaAni.Net.WebSockets.Server.DefineInfos;

public class HandlerArgDefineInfo
{
    public HandlerArgDefineInfo(ParameterInfo info)
    {
        Type = info.ParameterType;
        Name = info.Name!;
        var internalAttrs = info.GetCustomAttributes<MessageArgAttribute>();
        var isRequired = false;

        foreach (var attribute in internalAttrs)
        {
            if (attribute is RequiredAttribute)
            {
                isRequired = true;
                continue;
            }
            Category= attribute.Category;
            IsRequired=attribute.IsRequired;
            AdapterRegisterAttributeType = attribute.GetType();
            if (attribute.Name != null)
            {
                Name = attribute.Name;
            }
        }
        if (!IsRequired && isRequired)
        {
            IsRequired = true;
        }
    }

    public Type Type { get; set; }
    public string Name { get; set; }
    public ArgCategory Category { get; set; }
    public bool IsRequired { get; set; }
    public IMessageArgAdapter? Adapter { get; set; }
    public Type? AdapterRegisterAttributeType { get; set; }
}